#include "customer_record.h"
#include "ui_customer_record.h"

#include <QCalendarWidget>
#include "log/log_init.h"
#include "dbio/db_manager.h"
CustomerRecord::CustomerRecord(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CustomerRecord)
{
    ui->setupUi(this);
    model = new QSqlQueryModel(this);
    model->setQuery(QString(DEFAULT_CUSTOMER_SQL));
    ui->tableView->setModel(model);
    model->setHeaderData(0,Qt::Horizontal,tr("部门"));
    model->setHeaderData(1,Qt::Horizontal,tr("日期"));
    model->setHeaderData(2,Qt::Horizontal,tr("定位人员"));
    model->setHeaderData(3,Qt::Horizontal,tr("电话"));
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//是否可以编辑
    ui->tableView->setStyleSheet("QHeaderView::section { background-color: rgb(255, 186, 67) }");
    setTimeEdit();
    lastQuery = DEFAULT_CUSTOMER_SQL;
}

CustomerRecord::~CustomerRecord()
{
    delete ui;
}

void CustomerRecord::on_export_btn_clicked()
{
    exportExcle.Table2ExcelByHtml(ui->tableView,"sosplus");
}

void CustomerRecord::on_update_btn_clicked()
{
    model->setQuery(QString(DEFAULT_CUSTOMER_SQL));
    lastQuery = DEFAULT_CUSTOMER_SQL;
    ui->vipLv_check->setChecked(0);
    ui->sureuser_check->setChecked(0);
    ui->time_check->setChecked(0);
}

void CustomerRecord::setTimeEdit()
{
    QCalendarWidget *m_pCalendarWidget;
    m_pCalendarWidget = new QCalendarWidget(this);
    m_pCalendarWidget->setMinimumDate(QDate(2020,1,1));
    m_pCalendarWidget->setFixedSize(300,200); //日历控件的显示大小
    m_pCalendarWidget->show();

    ui->start_time->setCalendarPopup(true); //使用该句可以直接调用日历控件
    ui->start_time->setCalendarWidget(m_pCalendarWidget);
    ui->start_time->setDisplayFormat("yyyy/MM/dd");//yyyy/MM/dd
    ui->start_time->setDate(m_pCalendarWidget->selectedDate());
    ui->start_time->setDateRange(QDate(2021,1,1),
                                 m_pCalendarWidget->maximumDate());

    QCalendarWidget *m_pCalendarWidget_end;
    m_pCalendarWidget_end = new QCalendarWidget(this);
    m_pCalendarWidget_end->setMinimumDate(QDate(2020,1,1));
    m_pCalendarWidget_end->setFixedSize(300,200); //日历控件的显示大小
    m_pCalendarWidget_end->show();
    ui->end_time->setCalendarPopup(true); //使用该句可以直接调用日历控件
    ui->end_time->setCalendarWidget(m_pCalendarWidget_end);
    ui->end_time->setDisplayFormat("yyyy/MM/dd");//yyyy/MM/dd
    ui->end_time->setDate(m_pCalendarWidget_end->selectedDate());
    ui->end_time->setDateRange(QDate(2020,1,1),
                               m_pCalendarWidget_end->maximumDate());
    QDateTime current_date_time = QDateTime::currentDateTime();
    ui->end_time->setDate(current_date_time.date());

}

void CustomerRecord::on_select_clicked()
{
    QString query = "";

    if(ui->vipLv_check->isChecked()){//部门
        query += "cmf_dept.dept_name";
        query += " = ";
        query += "'"+ui->vipLv_lineEdit->text().simplified()+"'";
    }else if(ui->sureuser_check->isChecked()){//定位人
        query += "cmf_user.user_nickname";
        query += " = ";
        query += "'" +ui->sureuser_lineEdit->text().simplified() +"'";
    }else if(ui->time_check->isChecked()){//日期
        QString startYear = ui->start_time->sectionText(QDateTimeEdit::YearSection);
        QString startMonth = ui->start_time->sectionText(QDateTimeEdit::MonthSection);
        QString startDay = ui->start_time->sectionText(QDateTimeEdit::DaySection);

        QString endYear = ui->end_time->sectionText(QDateTimeEdit::YearSection);
        QString endMonth = ui->end_time->sectionText(QDateTimeEdit::MonthSection);
        QString endDay = ui->end_time->sectionText(QDateTimeEdit::DaySection);

        query += "cmf_orders.order_create_time between ";
        query += QString("'%1-%2-%3'").arg(startYear).arg(startMonth).arg(startDay);
        query += " and ";
        query += QString("'%1-%2-%3'").arg(endYear).arg(endMonth).arg(endDay);
    }
    if(!query.isEmpty()){
        model->setQuery(QString(DEFAULT_CUSTOMER_SQL) + " where "+query);
        lastQuery = QString(DEFAULT_CUSTOMER_SQL) + " where "+query;
        QLOG_INFO()<<"query = "<<QString(DEFAULT_CUSTOMER_SQL) + " where "+query;
    }
}

void CustomerRecord::on_clear_btn_clicked()
{
    model->setQuery(QString(DEFAULT_CUSTOMER_SQL));
    lastQuery = DEFAULT_CUSTOMER_SQL;
}

void CustomerRecord::on_create_table_btn_clicked()
{
    QMap<QString,int> showData;
    QList<QList<QString>> data =  DBManager::getInstance()->select(lastQuery);
    for(auto it : data){
        auto time = it.at(1);
        auto list = time.split("-");
        showData[list.at(0)+"."+list.at(1)]++;//1.1
    }

    signOtherChart(tr("临时表格"),showData);
}
